Page({

  data: {
    // 数据列表
    dataList: [
      {
        id: "01",
        navTitle: "推荐",
        goodsList: [{
          id: 1,
          goodsUrl: "/images/images-cyl/tx1.png",
          goodsName: "饮食健康组",
          saleNum: "病友分享自己健康饮食",
        },
        {
          id: 2,
          goodsUrl: "/images/images-cyl/tx1.png",
          goodsName: "病友艺术讨论组",
          saleNum: "病友可在这了解艺术魅力",
        },
        {
          id: 3,
          goodsUrl: "/images/images-cyl/tx1.png",
          goodsName: "病情分享与讨论组",
          saleNum: "病友分享自己病情",
        },
        {
          id: 4,
          goodsUrl: "/images/images-cyl/tx1.png",
          goodsName: "心理支持组",
          saleNum: "病友之间的互相关爱",
        },
        {
          id: 5,
          goodsUrl: "/images/images-cyl/tx1.png",
          goodsName: "病友情感宣泄组",
          saleNum: "分享的彼此之间的兴趣之事",
        }
        ],
      },
      {
        id: "02",
        navTitle: "医疗信息",
        goodsList: [{
          id: 21,
          goodsUrl: "/images/images-cyl/tx2.png",
          goodsName: "病情医生推荐组",
          saleNum: "病友分享自己觉得权威医生",
        },
        {
          id: 22,
          goodsUrl: "/images/images-cyl/tx2.png",
          goodsName: "医疗疑问解答组",
          saleNum: "病友可以提出关于疾病的疑问",
         
        },
        {
          id: 4,
          goodsUrl: "/images/images-cyl/tx2.png",
          goodsName: "病情专家咨询组",
          saleNum: "病情专家答疑",
        },
        {
          id: 5,
          goodsUrl: "/images/images-cyl/tx2.png",
          goodsName: "医院体验组",
          saleNum: "病友分享自己病情",
        }
        ],
      },
      {
        id: "03",
        navTitle: "健康生活",
        goodsList: [{
          goodsUrl: "/images/images-cyl/tx3.png",
          goodsName: "饮食健康组",
          saleNum: "病友分享自己健康饮食",
          id: 31,
        },
        {
          goodsUrl: "/images/images-cyl/tx3.png",
          goodsName: "运动健康组",
          saleNum: "病友可在这了解运动魅力",
          id: 32,
        },
        {
          id: 4,
          goodsUrl: "/images/images-cyl/tx3.png",
          goodsName: "睡眠健康组",
          saleNum: "了解健康知识和预防措施，提高健康素养",
        },
        {
          id: 5,
          goodsUrl: "/images/images-cyl/tx3.png",
          goodsName: "健康饮食与运动组",
          saleNum: "分享健康的饮食建议和食谱",
        },
        ],
      },
      {
        id: "04",
        navTitle: "心理支持",
        goodsList: [{
          goodsUrl: "/images/images-cyl/tx4.png",
          goodsName: "病情分享与讨论组",
          saleNum: "病友分享自己病情",
          id: 41,
        },
        {
          id: 4,
          goodsUrl: "/images/images-cyl/tx4.png",
          goodsName: "病情分享与讨论组",
          saleNum: "病友分享自己病情",
        },
        {
          id: 5,
          goodsUrl: "/images/images-cyl/tx4.png",
          goodsName: "病情分享与讨论组",
          saleNum: "病友分享自己病情",
        },
        ],
      },
      {
        id: "05",
        navTitle: "情感交流",
        goodsList: [{
          goodsUrl: "/images/images-cyl/tx5.png",
          goodsName: "病情分享与讨论组",
          saleNum: "病友分享自己病情",
          id: 61,
        },
        {
          goodsUrl: "/images/images-cyl/tx5.png",
          goodsName: "病情分享与讨论组",
          saleNum: "病友分享自己病情",
          id: 62,
        },
        ],
      },
      {
        id: "06",
        navTitle: "自我鼓励",
        goodsList: [{
          goodsUrl: "/images/images-cyl/tx6.png",
          goodsName: "心灵充电站",
          saleNum: "提供心灵上的充电和支持",
          id: 1,
        },
        {
          goodsUrl: "/images/images-cyl/tx6.png",
          goodsName: "勇气与坚持组",
          saleNum: "面对困难时保持坚韧不拔的精神",
          id: 2,
        },
        ],
      },
    ],
    searchHeight: 0,   // 搜索框高度
    currentActiveIndex: 0,   // 当前菜单索引
    isClickMenu: false, // 是否点击菜单
    proListToTop: [],  // 记录每一个类型的列表道顶部的距离
  },

  onLoad: function (options) {
    this.getEleHeight();
  },

  getEleHeight() {
    var searchHeight, proListToTop = [];

    // 搜索框所占据的高度，在后续滑动时需要减掉该高度
    wx.createSelectorQuery().select("#searchBox").boundingClientRect(res => {
      searchHeight = res.height
      // 回调函数是异步的，所以需要在方法里面更新数据
      this.setData({
        searchHeight: searchHeight,
      })
    }).exec()

    // 记录每一个类型的列表道顶部的距离
    wx.createSelectorQuery().selectAll('.itemTitle').boundingClientRect((rects) => {
      rects.forEach((rect) => {
        proListToTop.push(rect.top.toFixed(2) - this.data.searchHeight);
      })
      this.setData({
        proListToTop: proListToTop,
      })
    }).exec()

  },

  //滚动触发
  scroll: function (e) {
    // 若滑动是点击菜单触发的，则不进行判断；
    if (!this.data.isClickMenu) {
      // 获取滚动的高度
      var scrollTop = e.detail.scrollTop;
      var proListToTop = this.data.proListToTop;
      var currentActiveIndex;
      for (let i = 0; i < proListToTop.length; i++) {
        // 根据滑动距离判断当前处于哪个菜单索引
        if (e.detail.scrollTop < proListToTop[i] && i !== 0 && e.detail.scrollTop > proListToTop[i - 1]) {
          this.setData({
            currentActiveIndex: i - 1,
          })
        }
      }
    }
    // 将点击菜单状态还原为false
    this.setData({
      isClickMenu: false,
    })
  },

  //点击左边菜单事件
  changeLeftMenu: function (e) {
    // 当前点击的导航对应的右列表id
    var rigId = e.currentTarget.dataset.id;
    // 当前导航索引
    var index = e.currentTarget.dataset.index;
    // 点击当前导航索引，不作处理
    if (this.data.currentActiveIndex === index) {
      return;
    }
    this.setData({
      rigId: rigId,
      // 设置选中id
      currentActiveIndex: index,
      // 是否点击
      isClickMenu: true
    })
  },

})
